//
//  RBEncryption.m
//  RBDemoProj
//
//  Created by Lan on 2019/2/15.
//  Copyright © 2019年 SummerTea. All rights reserved.
//

#import "RBEncryption.h"
#import <CocoaSecurity.h>
#import "NSString+RBAdd.h"
#import "RSA.h"

@implementation RBEncryption

#pragma mark - AES

+ (NSString *)aesKey
{
    return @"a8237s727fc4oc8q";
}

+ (NSString *)aesIv
{
    return @"9sd7wwc5qe8cqw85";
}

NSString *aesHexKey;
+ (NSString *)aesHexKey
{
    if (!aesHexKey)
    {
        aesHexKey = [[self aesKey] hexString];
    }
    return aesHexKey;
}

NSString *aesHexIv;
+ (NSString *)aesHexIv
{
    if (!aesHexIv)
    {
        aesHexIv = [[self aesIv] hexString];
    }
    return aesHexIv;
}

+ (NSString *)aesEncrypt: (NSString *)text
{
    NSString *hexKey = [self aesHexKey];
    NSString *hexIv = [self aesHexIv];
    CocoaSecurityResult *result = [CocoaSecurity aesEncrypt:text hexKey:hexKey hexIv:hexIv];
    return result.base64;
}

+ (NSString *)aesDecrypt: (NSString *)text
{
    NSString *hexKey = [self aesHexKey];
    NSString *hexIv = [self aesHexIv];
    CocoaSecurityResult *result = [CocoaSecurity aesDecryptWithBase64:text hexKey:hexKey hexIv:hexIv];
    return result.utf8String;
}

#pragma mark - RSA

+ (NSString *)rsaPublicKey
{
    return @"-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDAwXWzzcTix0Lg7X5vjpXwjV3hzGnMAF6P1N/cAxuuLrtRa2g4ZqnteHFjED7TA+tbhVW5A/0Q22iytm+gJG392F3FcQqa4cbRSkwQHOg+yT05IYJ0+I+jdrTnNZCvhWyEa+TctM4c6HCE0VzD4xXUPIjCbTG1wqMbl1Rh+yQeaQIDAQAB-----END PUBLIC KEY-----";
}

+ (NSString *)rsaPrivateKey
{
    return @"-----BEGIN PRIVATE KEY-----MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMDBdbPNxOLHQuDtfm+OlfCNXeHMacwAXo/U39wDG64uu1FraDhmqe14cWMQPtMD61uFVbkD/RDbaLK2b6Akbf3YXcVxCprhxtFKTBAc6D7JPTkhgnT4j6N2tOc1kK+FbIRr5Ny0zhzocITRXMPjFdQ8iMJtMbXCoxuXVGH7JB5pAgMBAAECgYA8g8APHffE9qfhc84w+cPLDKa/v+wrFqaZY0yQhw1Gpvc5/Y1vv5NzsU9vuWbv2pgmWHrsJftqMmfxS2NKFIXPEoRLbxKO5XLZAdRiOrzzoZP+EYKnMu1VRtrDo3S3+TyJbhsJgF3cXpaN4fmdffAZ2V6WfKwa6iBnrr6sUEIa/QJBAPqCFR9+AJMrXbGv3EcXCoe7BrC1PNsQhbn+fp0k/FzYyTmlWiXuTzolCQqQFGheepaznoBbCCwAL1TPZJIppVsCQQDE+0FkGcxWX9jq7GebJkrVAQzu7rrZ/ZO2ao3lBf3DU+5s9Rj69LKDhOx1sc3oA6lPVBDcPuMpee2NTj48N+KLAkA+Q1C+uBGqgo/SFzS30qlCXSDnkNRkWnHybafIpSW4agVxt4B1hwrYF9Zscq0v17O+ezx9CKM6VH1vJfEIRyObAkEAndIhBi6VRO7uQOq/59K6SgbZceXb1yQP4StmHIS8WG86rBmDti2viShKu9GFg8rLMD/OcmgAYQ7LgVebDWxJGQJAJLzM+I9TbcnbAEbsfx6y/yb1OkdZCtE/m/qY5ns24kwmw1rKRrunvEsOO+Z0G+/Y/T76+w9ynt6pbN6g3a6zCA==-----END PRIVATE KEY-----";
}

+ (NSString *)rsaPublicKeyEncrypt: (NSString *)text;
{
    if (!text.length) return nil;
    return [RSA encryptString:text publicKey:[self rsaPublicKey]];
}

+ (NSString *)rsaPublicKeyDecrypt: (NSString *)text
{
    if (!text.length) return nil;
    return [RSA decryptString:text publicKey:[self rsaPublicKey]];
}

+ (NSString *)rsaPrivateKeyEncrypt: (NSString *)text
{
    if (!text.length) return nil;
    return [RSA encryptString:text privateKey:[self rsaPrivateKey]];
}

+ (NSString *)rsaPrivateKeyDecrypt: (NSString *)text
{
    if (!text.length) return nil;
    return [RSA decryptString:text privateKey:[self rsaPrivateKey]];
}

@end
